home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
checklpt.arc
/
CHKLPT.C
< prev
next >
Wrap
Text File
|
1987-04-19
|
9KB
|
195 lines
/***************************************************************************/
/* */
/* Program chklpt */
/* */
/* Usage: chklpt PRINTER */
/* where PRINTER is (upper, lower or mixed case) */
/* PRN, 1 or LPT1 for LPT1, */
/* 2 or LPT2 for LPT2 or */
/* 3 or LPT3 for LPT3. */
/* Returns ERRORLEVEL 0 if PRINTER is available, */
/* 1 if PRINTER is not available */
/* 100 for certain system errors. */
/* */
/* I designed chklpt to tell a batch file whether a printer is */
/* available. Unless there is trouble, it should run silently. In most */
/* cases of trouble, it gives the user a chance to tell the batch file */
/* about the printer status manually. */
/* chklpt requires DOS 2.0 or later to run. */
/* The ERRORLEVEL of 100 occurs only in the event of a compiler or */
/* system failure. */
/* */
/* I compiled chklpt and its subfunctions for use with Lattice C, */
/* Version 3. Some of their features which may not be portable are: */
/* fileno() is an UNIX function which returns the file handle for a */
/* file pointer. */
/* Lattice classes onerror(action) as a MSDOS function. onerror(1) */
/* preeempts the MSDOS critical error handler. onerror(0) restores the */
/* MSDOS critical error handler. */
/* getfc(fh, cw) returns 0 if fh is a valid file handle, -1 otherwise. */
/* If it is a valid file handle, the function sets cw to the */
/* word for the file. Bit 7 is 0 for a disk file. See DOS interrupt 44H, */
/* AL = 0, for more information about the characteristic word */
/* union REGS is a Lattice union which lets the registers become */
/* arguments to functions which invoke 8086 interrupts. Two functions */
/* use it. Both return an int containing the flags after the interrupt. */
/* intdos invokes interrupt 21H with input registers as the first */
/* argument and returns the value of the registers after the interrupt as */
/* the second argument. int86 invokes the interrupt specified by the */
/* first argument with input registers as the second argument. It returns */
/* the value of the registers after the interrupt as the third argument. */
/* For simpler interrupt 21H functions, Lattice has a function bdos, */
/* which takes at least one and up to three arguments. Its required */
/* first argument is the function number, which is the input value of AH. */
/* Its optional second argument is the input value of DX, and its optional */
/* third argument is the input value of AL. It returns the value of AX */
/* after the interrupt. */
/* yesorno.c uses an IBM ROM BIOS interrupt to handle the keyboard in a */
/* non-rediredctible way. Some clones may not accept it. */
/* */
/* Full identifiers in this program conform to the proposed ANSI */
/* standard of at most 31 significant characters, and they are different */
/* within the first 8 characters. Lattice C includes a -n compiler */
/* switch which changes the number of significant characters in */
/* identifiers from 8 to 39. While this program works without it, I */
/* designed them to use it. */
/* */
/* This file requires the functions in files CHECKLPT.C and YESORNO.C */
/* to compile and run */
/* */
/* Lew Paper */
/* 4/17/87 */
/***************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
/* Prototypes for external functions */
int FILE_check_lpt_available(FILE *);
int handle_check_lpt_available(int);
int yesorno(char *);
/* Prototypes for forward references */
int level2_check_lpt_available(char *);
void usage(void);
void fake_exit(void);
#define CHECK_NAME(LEGAL) !strcmpi(argv[1], "LEGAL")
#define CARRY_FLAG 1
main(argc, argv)
int argc;
char *argv[];
{
int not_available;
/* Check for DOS 2.0 or later */
if (!(bdos(0x30) & 0xff))
{
fputs("CHKLPT requires DOS 2.0 or later.\n\n", stderr);
fake_exit();
}
if (argc < 2)
usage(); /* Display usage and manually set */
/* ERRORLEVEL. Exit from there. */
/* Check for LPT1 availability with stdprn handle */
if (CHECK_NAME(prn) || CHECK_NAME(1) || CHECK_NAME(lpt1))
not_available = handle_check_lpt_available(4);
/* Open LPT2 or LPT3 as a level 2 file and check availability with its */
/* file pointer */
else if (CHECK_NAME(2) || CHECK_NAME(lpt2))
not_available = level2_check_lpt_available("LPT2");
else if (CHECK_NAME(3) || CHECK_NAME(lpt3))
not_available = level2_check_lpt_available("LPT3");
else
usage(); /* Display usage and manually set */
/* ERRORLEVEL. Exit from there. */
exit(not_available);
}
int level2_check_lpt_available(device_name)
char *device_name;
{
FILE *fp;
int handle;
int cw; /* Characteristic word for file */
int not_available;
/* PC DOS 3.1 opens printers which are off or not installed with */
/* no error indication, so I can not test a critical error here. I */
/* preempted the critical error handler because I am not sure that */
/* some version of DOS might not treat opening a printer which */
/* doesn't exist as a critical error. */
if (onerror(1)) /* Preempt MSDOS critical error */
/* handler. */
{
fputs("onerror(1) failed in level2_check_lpt_available\n\n", stderr);
exit(100);
}
if (!(fp = fopen(device_name, "w")))
{
fprintf(stderr, "Unable to open device %s\n\n", device_name);
fake_exit();
}
if (getfc((handle = fileno(fp)), &cw) || !(cw & 0x0080))
/* Invalid handle */
{
fprintf(stderr, "There is no device driver %s installed\n\n",
device_name);
fake_exit();
}
/* The Lattice C Manual doesn't say exactly how onerror() works. */
/* I turned it off here to protect its being turned back on in */
/* FILE_check_lpt_available */
if (onerror(0)) /* Restore MSDOS critical error */
{
fputs("onerror(0) failed in level2_check_lpt_available\n\n", stderr);
exit(100);
}
not_available = handle_check_lpt_available(handle);
if (fclose(fp))
fprintf(stderr, "\nError in closing device %s\n\n", device_name);
return not_available;
}
void usage()
{
fputs("Usage: chklpt PRINTER\n", stderr);
fputs(" where PRINTER is (upper, lower or mixed case)\n", stderr);
fputs(" PRN, 1 or LPT1 for LPT1,\n", stderr);
fputs(" 2 or LPT2 for LPT2 or\n", stderr);
fputs(" 3 or LPT3 for LPT3.\n", stderr);
fputs("Returns ERRORLEVEL 0 if PRINTER is available,\n", stderr);
fputs(" 1 if PRINTER is not available\n", stderr);
fputs(" 100 for certain system errors\n", stderr);
fake_exit();
}
void fake_exit()
{
fputs("\nYou manually determine the ERRORLEVEL for this invocation\n",
stderr);
if (yesorno("Do you want to indicate the printer is available (Y or N)?"))
exit(0);
else
exit(1);
}